//
//  PurchaseHistoryViewModel.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/23.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import RxDataSources
import RxSwift
import RxCocoa

class PurchaseHistoryViewModel {
    var disposeBag = DisposeBag()
    let vmData = Variable([PurchaseHistorySection]())
    
    var requstCommand = PublishSubject<Bool>()
    
    init() {
        self.requstCommand.subscribe(onNext: { (isPull) in
            self.purchaseHistory()
        }).disposed(by: disposeBag)
    }
    
    
    private func purchaseHistory() {
        startAnimating()
        shurenProvider.rx.request(.purchaseHistory).asObservable().mapRespnseToObjectArray(type: PurchaseHistoryModel.self).subscribe(onNext: { [weak self] (models) in
            let section = PurchaseHistorySection.init(items: models)
            self?.vmData.value = [section]
            }, onError: { (error) in
                showError(error)
                stopAnimating()
        }, onCompleted: {
            stopAnimating()
        }).disposed(by: disposeBag)
    }
}

struct PurchaseHistorySection {
    var items: [PurchaseHistoryModel]
}

extension PurchaseHistorySection: SectionModelType {
    typealias Item = PurchaseHistoryModel
    init(original: PurchaseHistorySection, items: [PurchaseHistoryModel]) {
        self = original
        self.items = items
    }
}

